package com.thread.local;

import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
import java.util.concurrent.TimeUnit;


public class A {
	static final ThreadLocal<String> threadParam = new ThreadLocal<String>();

	public static void main(String[] args) throws InterruptedException {
		
		ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
		// 死循环,测多几次看结果
		while (true) {
			// 线程1
			new Thread(new Runnable() {

				public void run() {
					// 设置参数
					threadParam.set("abc");
					// 输出参数
					System.out.println("t1:" + threadParam.get());
					// 看起来像是多余操作
					// threadParam.remove();
				}
			}).start();
			TimeUnit.SECONDS.sleep(1);
			new Thread(new Runnable() {
				public void run() {
					// 线程二,测试是否能获取abc
					System.out.println("t2:" + threadParam.get());
				}
			}).start();
			
			
			long[] ids = mxBean.getAllThreadIds();
			
			ThreadInfo[] infos = mxBean.getThreadInfo(ids);
			for (int i = 0; i < infos.length; i++) {
				if(infos[i] != null)
				System.out.println(infos[i].toString());
			}
			
		}
	}
}